<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="nespresso.beans.*" %>
<%@page import="java.sql.*" %>
<%@page import="java.text.*" %>
<html>
<head>
<title>NESPRESSO</title>

<link rel="stylesheet" type="text/css" href="css/jquery-ui-1.8.17.custom.css" />
<link rel="stylesheet" type="text/css" href="css/ui.jqgrid.css" />
<link rel="stylesheet" type="text/css" href="css/nespresso.css" />

<script src="js/jquery-1.7.2.min.js" type="text/javascript"></script>
<script src="js/grid.locale-fr.js" type="text/javascript"></script>
<script src="js/jquery.jqGrid.js" type="text/javascript"></script>
<script src="js/jquery.corners.js" type="text/javascript"></script>
<script src="js/date.js" type="text/javascript"></script>

</head>

<body>
<div align="center">
<div align="right" style="background:#82786e;">
<%
User currentUser = (User)session.getAttribute("user");
String enpreparation = new String("non");
%><br><%
if(currentUser.getRole().equals("ADMIN")) {
	%><a href="administration.jsp">Administration</a>&nbsp;&nbsp;&nbsp; <%
} %>
<a href="nespresso.jsp">Déconnexion</a><br><br></div>
<div id="divrond1">
	<table width="100%">
		<tr height="83px"><td width="50%"></td><td align="center">	
			<p class="grosblancitalique"><b>Liste des commandes</b></p>
		</td></tr>
	</table>
</div>

<form action="Commande" method="post" name="CommandesForm" target="_self">
<div align="center" class="divrondform">
<table style="width:800px">
  <tr style="width:610px"><td>
  <table id="TabCommande">
  <tr>
  	<th>Id</th>
  	<th>Date</th>
  	<th>Statut</th>
  	<th>Responsable</th>
  	<th>Montant</th>
  	<th>M</th>
  </tr>
  <tbody>
  	<%	Base bc = new Base(); Base bu = new Base();
  		String NomPrenom = new String();
  		bc.GetResultat("COMMANDES");
  		while(bc.Suivant()) {
  			bu.GetResultat("USERS", "ID", bc.GetTexte("ID_USER_RESP")); bu.Suivant();
  			NomPrenom = bu.GetTexte("PRENOM") + " " + bu.GetTexte("NOM"); %>
  			<tr>
  				<td><%=bc.GetTexte("ID") %></td>
  				<td><%=bc.GetTexte("DATE") %></td>
  				<td><%=bc.GetTexte("STATUT") %></td>
  				<td><%=NomPrenom %></td>
  				<td><%=new DecimalFormat("0.00").format(bc.GetReel("MONTANT")) %></td>
  				<td><input class="c_hand" style="background-image:url(images/crayon.png); border:none; width:30px; background-color:transparent;" value="" type="submit" id="m<%=bc.GetTexte("ID")%>" name="modif<%=bc.GetTexte("ID")%>" title="Modifier la commande N° <%=bc.GetTexte("ID")%>"></td>
  			</tr>
  			<%
  		}
  		if(bc.IsCommandeEnPreparation()) { enpreparation = "oui"; };
  		bu.Free(); bc.Free();
  	%>
  </tbody>
  </table>
  </td>
  <td valign="top" align="center">
  	<input type="hidden" id="en-preparation" value="<%=enpreparation %>" /> 
    <input type="submit" name="bcommande" id="bcNew" value="Nouvelle commande" class="boutons" /><br><br>
    <input type="submit" name="bcommande" id="bcMod" value="Modifier commande" class="boutons" /><br><br>
    <input type="submit" name="bcommande" id="bcRec" value="Récapitulatif" class="boutons" /><br>
    </td>
  </tr></table>
</div>
<input type="hidden" id="nCommande" name="nCommande" value="NULL" />
</form>
</div>
<script>

$("#divrond1").corner("round 8px");
$(".divrondform").corner("round 8px");

function formatcommande(cellvalue, options, rowobject) {
	var val = cellvalue;
	var nom = options.colModel.name;
	var datecommande;
	
	if(nom == "Montant") val = val + " €";
	if(nom == "Date") {
		datecommande = Date.parseExact(val, "yyyy-MM-dd");
		val = datecommande.toString("dd/MM/yyyy");
	}
	return '<span class="c_pointeur">' + val + '</span>';
}

function restovaleur(cellvalue, options, cell) {
	return $(cell).text();
}


tableToGrid("#TabCommande", {
    width: 'auto',
    height: "auto",
    hidegrid: false,
    sortname: "Date",
    sortorder: "desc",
	colModel: [{name:'Id', editable:false, width:80, formatter:formatcommande, unformat:restovaleur},
	           {name:'Date', editable:false, width:110, formatter:formatcommande, unformat:restovaleur},
               {name:'Statut', editable:false, width:170, formatter:formatcommande, unformat:restovaleur},
               {name:'Responsable', editable:false, width:280, formatter:formatcommande, unformat:restovaleur},
               {name:'Montant', editable:false, width:120, align:'right', formatter:formatcommande, unformat:restovaleur},
               {name:'M', editable:false, width:40, align:'center'}],
    onSelectRow: function(id) {
        $("#nCommande").val($("#TabCommande").getCell(id, 'Id'));
    },
    ondblClickRow: function(id) {
    	$("#nCommande").val($("#TabCommande").getCell(id, 'Id'));
    	$("#bcMod").click();
    }
});

// Griser le bouton "nouvelle commande" ?
if($("#en-preparation").val() == "oui") {
	$("#bcNew").attr("disabled", "disabled");
}

$("#TabCommande").trigger("sortGrid");
$("#TabCommande").trigger("reloadGrid");

</script>
</body>
</html>
